package com.xydtech.ecard.ecard.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xydtech.common.annotation.RsaRequest;
import com.xydtech.common.core.domain.entity.SysDept;
import com.xydtech.common.enums.ResultStatus;
import com.xydtech.common.model.ResponseModel;
import com.xydtech.common.utils.DateUtils;
import com.xydtech.common.utils.GeoUtil;
import com.xydtech.common.utils.StringUtils;
import com.xydtech.ecard.config.service.IEcardApiSysConfigService;
import com.xydtech.ecard.ecard.domain.*;
import com.xydtech.ecard.ecard.service.*;
import com.xydtech.ecard.merchant.domain.*;
import com.xydtech.ecard.merchant.service.IEcardBranchPersonChartService;
import com.xydtech.ecard.user.domain.EcardApiEcardRegistInfo;
import com.xydtech.ecard.user.domain.EcardApiUserInfo;
import com.xydtech.ecard.user.service.IEcardApiEcardRegistInfoService;
import com.xydtech.ecard.user.service.IEcardApiUserService;
import com.xydtech.system.domain.SysConfig;
import com.xydtech.system.domain.SysWechatApplet;
import com.xydtech.system.service.ISysConfigService;
import com.xydtech.system.service.ISysDeptService;
import com.xydtech.system.service.ISysUserService;
import com.xydtech.system.service.ISysWechatAppletService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 电子名片-个人营销接口类
 *
 * @author yuzhiguo
 * @date 2024-08-26 下午2:37
 */
@Controller
@Slf4j
@Api(tags = "电子名片-个人营销接口")
@RequestMapping("/ecardSaasApi/personMarketing")
public class EcardPersonMarketingController extends EcardApiBaseController {

    //4:客户经理
    private static String roles = "4";
    //客户经理岗位，后台没维护，这里用不到
    private static String cm = "cm";
    //网格管理员岗位
    private static String wg = "wg";

    @Autowired
    private IEcardApiUserService ecardApiUserService;
    @Autowired
    private IEcardApiPersonInfoService ecardPersonInfoService;
    @Autowired
    private IEcardApiEcardRegistInfoService registInfoService;
    @Autowired
    private IEcardApiMerchantVisitInfoService ecardMerchantVisitInfoService;
    @Autowired
    private ISysWechatAppletService wechatAppletService;
    @Autowired
    private IEcardApiSysConfigService ecardApiSysConfigService;
    @Autowired
    private IEcardApiMerchantTaskService ecardApiMerchantTaskService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IEcardApiMerchantPartInfoService ecardMerchantPartInfoService;
    @Autowired
    private IEcardApiPersonManageWorkInfoService ecardPersonManageWorkInfoService;
    @Autowired
    private IEcardApiPersonAreaService ecardPersonAreaService;
    @Autowired
    private ISysDeptService sysDeptService;
    @Autowired
    private ISysConfigService configService;
    @Autowired
    private IEcardBranchPersonChartService ecardBranchPersonChartService;
    @Autowired
    private ISysConfigService sysConfigService;

    /**
     * 电子名片-条件查询登录人名下个人列表接口
     *
     * @author yuzhiguo
     * @date 2024/8/26 下午4:00
     */
    @PostMapping(value = "/getMyPersonList")
    @ApiOperation(value = "电子名片-条件查询登录人名下个人列表接口", notes = "电子名片-条件查询登录人名下个人列表接口")
    @ResponseBody
    @RsaRequest
    public ResponseModel getMyPersonList(@RequestBody String json) {
        log.info("接口请求时间-------" + DateUtils.getTime());
        try {
            String salt = getSalt();//盐值
            String currentUserId = getCurrentUserId();
            JSONObject jsonObject = JSONObject.parseObject(json);
            String search = jsonObject.getString("search");//查询条件姓名和手机号拆开（客户姓名/联系人姓名）
            String phone = jsonObject.getString("phone");
            Integer perType = jsonObject.getInteger("perType");//个人客户类型，1：农户，2：新市民
            String areaName = jsonObject.getString("areaName");//网格名称
            Long perAreaId = jsonObject.getLong("perAreaId");//网格id
            int pageNum = jsonObject.getInteger("pageNum");
            int pageSize = jsonObject.getInteger("pageSize");

            EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(currentUserId, salt);
            //当前小程序id
            SysWechatApplet wechatApplet = getAppIdByDeptId(userInfo.getDeptId());

            EcardApiPersonInfo ecardPersonInfo = new EcardApiPersonInfo();
            ecardPersonInfo.setPerCustName(search);
            ecardPersonInfo.setContactsPhone(phone);
            ecardPersonInfo.setPerType(perType);
            ecardPersonInfo.setManagementPersonnelId(Long.valueOf(currentUserId));
            ecardPersonInfo.setAreaName(areaName);
            ecardPersonInfo.setPerAreaId(perAreaId);
            ecardPersonInfo.setDictDeptId(wechatApplet.getDeptId());
            PageHelper.startPage(pageNum, pageSize);
            List<EcardApiPersonInfo> personList = ecardPersonInfoService.getMyPersonList(ecardPersonInfo, salt);
            List<EcardApiPersonInfo> apiPersonList = new ArrayList<>();
            PageInfo pageInfo = new PageInfo(personList);
            for (EcardApiPersonInfo person : personList) {
                //查询总拜访次数
                EcardApiMerchantVisitInfo merchantVisitInfo = new EcardApiMerchantVisitInfo();
                merchantVisitInfo.setMerchantId(person.getId());
                merchantVisitInfo.setMerchantType("1");//商户类型：1-个人，2-商户，3-企业，4-新型农业主体
                List<EcardApiMerchantVisitInfo> visitInfoList = ecardMerchantVisitInfoService.getMerchantVisitList(merchantVisitInfo);
                person.setVisitNumber(visitInfoList.size() + "");

                //查询最近一次拜访时间
                String lastVisitTime = ecardMerchantVisitInfoService.getMerchantLastVisitTime(person.getId(), "1");//商户类型：1-个人，2-商户，3-企业，4-新型农业主体
                if (StringUtils.isNotEmpty(lastVisitTime)) {
                    person.setLastVisitDate(DateUtils.parseDate(lastVisitTime));
                }
                apiPersonList.add(person);
            }
            log.info("接口返回时间-------" + DateUtils.getTime());
            return ResponseModel.okToJSONArray(pageInfo.getTotal(), apiPersonList);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败，请联系管理员！");
        }
    }

    /**
     * 电子名片-条件查询个人列表接口
     *
     * @param: type 1登录人所在机构下全部，2法人机构下全部（针对网格管理岗位）
     * @param: search 查询条件
     * @author yuzhiguo
     * @date 2024/8/26 下午5:07
     */
    @PostMapping(value = "/getPersonList")
    @ApiOperation(value = "电子名片-条件查询个人列表接口", notes = "电子名片-条件查询个人列表接口")
    @ResponseBody
    @RsaRequest
    public ResponseModel getPersonList(@RequestBody String json) {
        log.info("接口请求时间-------" + DateUtils.getTime());
        try {
            String salt = getSalt();//盐值
            String currentUserId = getCurrentUserId();
            //查询客户经理信息
            EcardApiEcardRegistInfo ecardApiEcardRegistInfo = registInfoService.selectEcardApiEcardRegistInfoByRegistId(Long.valueOf(currentUserId), salt);
            JSONObject jsonObject = JSONObject.parseObject(json);
            String type = jsonObject.getString("type");
            String search = jsonObject.getString("search");//查询条件姓名和手机号拆开（客户姓名/联系人姓名/联系人手机号）
            String phone = jsonObject.getString("phone");
            Integer perType = jsonObject.getInteger("perType");//个人客户类型，1：农户，2：新市民
            String areaName = jsonObject.getString("areaName");//网格名称
            Long perAreaId = jsonObject.getLong("perAreaId");//网格id
            String sortTime = jsonObject.getString("sortTime");
            int pageNum = jsonObject.getInteger("pageNum");
            int pageSize = jsonObject.getInteger("pageSize");

            //获取当前用户的信息
            EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(currentUserId, salt);
            EcardApiPersonInfo ecardPersonInfo = new EcardApiPersonInfo();
            ecardPersonInfo.setPerCustName(search);
            ecardPersonInfo.setContactsPhone(phone);
            ecardPersonInfo.setPerType(perType);
            ecardPersonInfo.setAreaName(areaName);
            ecardPersonInfo.setPerAreaId(perAreaId);
            ecardPersonInfo.setSortTime(sortTime);
            //当前小程序id
            SysWechatApplet wechatApplet = getAppIdByDeptId(userInfo.getDeptId());

            ecardPersonInfo.setDictDeptId(wechatApplet.getDeptId());
            if (ecardApiEcardRegistInfo != null) {
                boolean postFlag = false;
                //多个岗位判断，只要有网格管理就传法人机构
                if (!StringUtils.objIsNullOrEmpty(userInfo.getExtendPostCode())) {
                    for (String extendPost : userInfo.getExtendPostCode().split(",")) {
                        if (wg.equals(extendPost)) {
                            postFlag = true;
                        }
                    }
                }
                if (roles.equals(ecardApiEcardRegistInfo.getRoles())) {
                    if ("1".equals(type) && !postFlag) {
                        ecardPersonInfo.setDeptId(userInfo.getDeptId());
                    } else if (postFlag) {
                        ecardPersonInfo.setDeptId(wechatApplet.getDeptId());
                    } else {
                        return ResponseModel.error("操作人岗位不正确,请联系管理员！");
                    }
                } else {
                    return ResponseModel.error("您没有查看个人列表的权限！");
                }
            } else {
                return ResponseModel.error("用户信息不存在！");
            }
            PageHelper.startPage(pageNum, pageSize);
            List<EcardApiPersonInfo> personList = ecardPersonInfoService.getPersonList(ecardPersonInfo, salt);
            PageInfo pageInfo = new PageInfo(personList);
            log.info("接口返回时间-------" + DateUtils.getTime());
            return ResponseModel.okToJSONArray(pageInfo.getTotal(), personList);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败，请联系管理员！");
        }
    }

    /**
     * 电子名片-查询个人地图坐标列表接口
     *
     * @param: type 1登录人所在机构下全部，2法人机构下全部（针对网格管理岗位）
     * @author yuzhiguo
     * @date 2024/8/26 下午5:23
     */
    @PostMapping(value = "/getPersonCoordinateList")
    @ApiOperation(value = "电子名片-查询个人地图坐标列表接口", notes = "电子名片-查询个人地图坐标列表接口")
    @ResponseBody
    @RsaRequest
    public ResponseModel getPersonCoordinateList(@RequestBody String json) {
        try {
            String salt = getSalt();//盐值
            String currentUserId = getCurrentUserId();
            JSONObject jsonObject = JSONObject.parseObject(json);
            String type = jsonObject.getString("type");
            Integer perType = jsonObject.getInteger("perType");//个人客户类型，1：农户，2：新市民
            String coordinate = jsonObject.getString("coordinate");

            SysConfig config = new SysConfig();
            config.setConfigKey("ecard_merchant_show_distance");
            SysConfig sysConfig;

            if (StringUtils.objIsNullOrEmpty(currentUserId) || StringUtils.objIsNullOrEmpty(type)) {
                return ResponseModel.error(ResultStatus.PARAMETER_MISSING);
            }
            List<EcardApiPersonInfo> personInfoList;
            if ("1".equals(type)) {
                //获取当前用户的信息
                EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(currentUserId, salt);
                personInfoList = ecardPersonInfoService.getPersonCoordinateList(Long.valueOf(currentUserId), userInfo.getDeptId(), perType);
                SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(userInfo.getDeptId());
                config.setDeptId(sysDept.getDeptId());
                sysConfig = ecardApiSysConfigService.selectConfig(config);
            } else {
                EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(currentUserId, salt);
                //当前小程序id
                SysWechatApplet wechatApplet = getAppIdByDeptId(userInfo.getDeptId());
                personInfoList = ecardPersonInfoService.getAllPersonCoordinateList(Long.valueOf(currentUserId), wechatApplet.getDeptId(), perType);
                SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(wechatApplet.getDeptId());
                config.setDeptId(sysDept.getDeptId());
                sysConfig = ecardApiSysConfigService.selectConfig(config);
            }

            //有经纬度就计算，没有取全部
            if (coordinate != null) {
                List<EcardApiPersonInfo> list = new ArrayList<>();
                //计算是否在配置的范围之内
                //定位的个人经纬度
                String place[] = coordinate.split(",");
                for (EcardApiPersonInfo ecardApiPersonInfo : personInfoList) {
                    String clockInPlace[] = ecardApiPersonInfo.getManageCoordinate().split(",");
                    Double distance = GeoUtil.GetDistance(Double.valueOf(clockInPlace[1]), Double.valueOf(clockInPlace[0]), Double.valueOf(place[1]), Double.valueOf(place[0]));

                    BigDecimal bigDistance = new BigDecimal(distance);
                    BigDecimal bigValue = new BigDecimal(sysConfig.getConfigValue());
                    if (bigDistance.compareTo(bigValue) <= 0) {
                        list.add(ecardApiPersonInfo);
                    }
                }
                return ResponseModel.okToJSONArray(list);
            } else {
                return ResponseModel.okToJSONArray(personInfoList);
            }

        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败，请联系管理员！");
        }
    }

    /**
     * 电子名片-保存个人数据接口,只有客户经理能保存个人信息
     *
     * @author yuzhiguo
     * @date 2024/8/26 下午2:47
     */
    @PostMapping(value = "/addPersonInfo")
    @ApiOperation(value = "电子名片-保存个人数据接口", notes = "电子名片-保存个人数据接口")
    @ResponseBody
    @RsaRequest(result = false)
    public ResponseModel addPersonInfo(@RequestBody EcardApiPersonInfo ecardPersonInfo) {
        try {
            String salt = getSalt();//盐值
            String currentUserId = getCurrentUserId();
            Long perId = ecardPersonInfo.getId();//个人id
            boolean flag = false;//判断是新增还是编辑，true：新增，false：编辑
            EcardApiEcardRegistInfo ecardApiEcardRegistInfo = registInfoService.selectEcardApiEcardRegistInfoByRegistId(Long.valueOf(currentUserId), salt);
            EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(currentUserId, salt);
            if (!roles.equals(ecardApiEcardRegistInfo.getRoles())) {
                return ResponseModel.error("您没有编辑个人信息的权限！");
            }
            //当前小程序id
            SysWechatApplet wechatApplet = getAppIdByDeptId(userInfo.getDeptId());

            //当前登录人ID
            ecardPersonInfo.setCreatorId(Long.valueOf(currentUserId));
            //管户人id
            ecardPersonInfo.setManagementPersonnelId(Long.valueOf(currentUserId));
            //登录人机构ID
            ecardPersonInfo.setDeptId(userInfo.getDeptId());
            if (!StringUtils.isNotNull(ecardPersonInfo.getId())) {
                //校验个人重复建档：同一客户类型下不允许重复建档
                int count = ecardPersonInfoService.validatePersonInfo(ecardPersonInfo);
                if (count > 0) {
                    return ResponseModel.error("相同客户类型、支行、客户姓名不能重复！");
                }
                flag = true;
            } else {
                //校验是否是自己的个人客户
                EcardApiPersonInfo ecardApiPersonInfo = ecardPersonInfoService.selectEcardPersonInfoById(ecardPersonInfo.getId(), salt);
                if (ecardApiPersonInfo != null) {
                    if (ecardApiPersonInfo.getManagementPersonnelId().longValue() != ecardApiEcardRegistInfo.getRegistId().longValue()) {
                        ResponseModel.error("您没有编辑该个人信息的权限！");
                    }
                } else {
                    return ResponseModel.error("操作失败，未查询到该个人信息！");
                }
                flag = false;
            }
            int id = ecardPersonInfoService.addPersonInfo(ecardPersonInfo, salt);

            //处理成员信息（新增/编辑）
            List<EcardApiMerchantPartInfo> ecardApiMerchantPartInfos = ecardPersonInfo.getEcardApiMerchantPartInfos();
            if (ecardApiMerchantPartInfos.size() > 0) {
                for (EcardApiMerchantPartInfo ecardMerchantPartInfo : ecardApiMerchantPartInfos) {
                    //查询个人人员是否存在
                    if (!StringUtils.objIsNullOrEmpty(ecardMerchantPartInfo.getId())) {
                        EcardApiMerchantPartInfo ecardApiMerchantPartInfo = ecardMerchantPartInfoService.selectEcardMerchantPartInfoById(ecardMerchantPartInfo.getId(), salt);
                        if (ecardApiMerchantPartInfo == null) {
                            return ResponseModel.error("成员信息不存在，请核实！");
                        }
                    }
                    //当前登录人Id
                    ecardMerchantPartInfo.setCreatorId(Long.valueOf(currentUserId));
                    ecardMerchantPartInfo.setMerchantType("1");//商户类型：1-个人，2-商户，3-企业，4-新型农业主体
                    if (flag) {//新增时取保存完个人返回的个人id，编辑时取传参的个人id
                        ecardMerchantPartInfo.setMerchantId(ecardPersonInfo.getId());
                    } else {
                        ecardMerchantPartInfo.setMerchantId(perId);
                    }
                    //添加时根据个人id和商户类型判断有没有成员，没有把是否联系人字段改为是，有不操作
                    List<EcardApiMerchantPartInfo> ecardMerchantPartInfos = ecardMerchantPartInfoService.getMerchantPartList(ecardMerchantPartInfo.getMerchantId(), "1", wechatApplet.getDeptId(), salt);
                    if (ecardMerchantPartInfos.size() == 0) {
                        ecardMerchantPartInfo.setIsContacts("T");
                    }
                    ecardMerchantPartInfoService.addMerchantPartInfo(ecardMerchantPartInfo, salt);
                }
            }
            //处理经营工作信息（新增/编辑）
            EcardApiPersonManageWork ecardApiPersonManageWork = ecardPersonInfo.getEcardApiPersonManageWork();
            if (ecardApiPersonManageWork != null) {
                if (flag) {//新增时取保存完个人返回的个人id，编辑时取传参的个人id
                    ecardApiPersonManageWork.setManagePerId(ecardPersonInfo.getId());
                } else {
                    ecardApiPersonManageWork.setManagePerId(perId);
                }
                //处理个人客户类型
                ecardApiPersonManageWork.setPerType(ecardPersonInfo.getPerType());//个人客户类型，1：农户，2：新市民
                ecardPersonManageWorkInfoService.addPersonManageWorkInfo(ecardApiPersonManageWork);

            }

            if (flag) {//只有新增个人时添加拜访任务
                //查询参数配置-拜访任务定时天数
                SysConfig config = new SysConfig();
                config.setConfigKey("ecard_merchant_task_days");
                SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(userInfo.getDeptId());
                config.setDeptId(sysDept.getDeptId());
                SysConfig sysConfig = ecardApiSysConfigService.selectConfig(config);
                int taskDays = Integer.valueOf(sysConfig.getConfigValue());//拜访任务定时天数
                /**
                 * 保存个人时新增一条拜访任务，下次回访日期默认为当前日期加拜访任务定时天数
                 */
                Date nextDay = DateUtils.getNextDay(new Date(), taskDays);
                Date endDate = DateUtils.formatDate(nextDay, DateUtils.YYYY_MM_DD);
                EcardApiMerchantTask merchantTask = new EcardApiMerchantTask();
                merchantTask.setMerchantId(ecardPersonInfo.getId());
                merchantTask.setCmRegistId(ecardPersonInfo.getManagementPersonnelId());
                merchantTask.setMerchantType("1");//商户类型：1-个人，2-商户，3-企业，4-新型农业主体
                merchantTask.setEndDate(endDate);
                merchantTask.setTaskSts("0");
                ecardApiMerchantTaskService.insertEcardMerchantTask(merchantTask);
            }
            return id > 0 ? ResponseModel.ok(ecardPersonInfo.getId()) : ResponseModel.error("添加失败！");
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败，请联系管理员！");
        }
    }

    /**
     * 电子名片-查询个人详情接口
     *
     * @author yuzhiguo
     * @date 2024/8/27 上午9:12
     */
    @PostMapping("/getPersonInfo")
    @ApiOperation(value = "电子名片-查询个人详情接口", notes = "电子名片-查询个人详情接口")
    @ResponseBody
    @RsaRequest
    public ResponseModel getPersonInfo(@RequestBody String json) {
        try {
            String salt = getSalt();//盐值
            String currentUserId = getCurrentUserId();
            EcardApiEcardRegistInfo ecardApiEcardRegistInfo = registInfoService.selectEcardApiEcardRegistInfoByRegistId(Long.valueOf(currentUserId), salt);
            if (!roles.equals(ecardApiEcardRegistInfo.getRoles())) {
                return ResponseModel.error("您没有查看该个人的权限！");
            }

            JSONObject jsonObject = JSONObject.parseObject(json);
            Long id = jsonObject.getLong("id");
            if (StringUtils.objIsNullOrEmpty(id)) {
                return ResponseModel.error(ResultStatus.PARAMETER_MISSING);
            }
            //根据个人id查询个人成员信息
            EcardApiMerchantPartInfo apiMerchantPartInfo = new EcardApiMerchantPartInfo();
            apiMerchantPartInfo.setMerchantId(id);
            apiMerchantPartInfo.setMerchantType("1");
            List<EcardApiMerchantPartInfo> merchantPartList = ecardMerchantPartInfoService.selectEcardMerchantPartInfoList(apiMerchantPartInfo, salt);//商户类型：1-个人，2-商户，3-企业，4-新型农业主体

            //根据个人id查询个人经营/工作信息
            EcardApiPersonManageWork apiPersonManageWorkInfo = new EcardApiPersonManageWork();
            apiPersonManageWorkInfo.setManagePerId(id);
            List<EcardApiPersonManageWork> personManageWorkList = ecardPersonManageWorkInfoService.selectEcardPersonManageWorkList(apiPersonManageWorkInfo);

            EcardApiPersonInfo personInfo = ecardPersonInfoService.selectEcardPersonInfoById(id, salt);

            if (merchantPartList.size() > 0) {
                personInfo.setEcardApiMerchantPartInfos(merchantPartList);//成员信息
            }
            if (personManageWorkList.size() > 0) {
                personInfo.setEcardApiPersonManageWork(personManageWorkList.get(0));//经营/工作信息
            }
            return ResponseModel.okToJSON(personInfo);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败，请联系管理员！");
        }
    }

    /**
     * 电子名片-根据ID修改个人状态
     *
     * @author yuzhiguo
     * @date 2024/8/27 上午9:13
     */
    @PostMapping(value = "/changePersonStatus")
    @ApiOperation(value = "电子名片-根据ID修改个人状态", notes = "电子名片-根据ID修改个人状态")
    @ResponseBody
    @RsaRequest(result = false)
    public ResponseModel changePersonStatus(@RequestBody String json) {
        try {
            JSONObject jsonObject = JSONObject.parseObject(json);
            Long id = jsonObject.getLong("id");
            if (StringUtils.objIsNullOrEmpty(id)) {
                return ResponseModel.error(ResultStatus.PARAMETER_MISSING);
            }
            int updateCount = ecardPersonInfoService.changePersonStatus(id);
            return updateCount > 0 ? ResponseModel.ok() : ResponseModel.error("删除失败！");
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败，请联系管理员！");
        }
    }

    /**
     * 电子名片-保存网格信息数据接口
     *
     * @author yuzhiguo
     * @date 2024/8/27 上午9:29
     */
    @PostMapping(value = "/addPersonAreaInfo")
    @ApiOperation(value = "电子名片-保存网格信息数据接口", notes = "电子名片-保存网格信息数据接口")
    @ResponseBody
    @RsaRequest(result = false)
    public ResponseModel addPersonAreaInfo(@RequestBody EcardApiPersonArea ecardApiPersonArea) {
        try {
            String salt = getSalt();//盐值
            String currentUserId = getCurrentUserId();
            EcardApiEcardRegistInfo ecardApiEcardRegistInfo = registInfoService.selectEcardApiEcardRegistInfoByRegistId(Long.valueOf(currentUserId), salt);
            EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(currentUserId, salt);
            //查询支行机构下的所有网格
            SysDept sysDept = sysDeptService.selectThirdLevelDeptByDeptId(userInfo.getDeptId());

            if (!StringUtils.isNotNull(ecardApiPersonArea.getId())) {
                //保存时校验该支行机构下是否已经有同名的网格
                List<EcardApiPersonArea> personAreas = ecardPersonAreaService.selectPersonAreaByAreaName(ecardApiPersonArea.getAreaName(), sysDept.getDeptId(), salt);
                if (personAreas.size() > 0) {
                    return ResponseModel.error("已存在同名的网格信息！");
                }
            } else {
                //编辑时校验该网格是否在本人名下
                EcardApiPersonArea personArea = ecardPersonAreaService.selectEcardPersonAreaById(ecardApiPersonArea.getId(), salt);
                if (personArea != null) {
                    if (personArea.getManagementPersonnelId().longValue() != ecardApiEcardRegistInfo.getRegistId().longValue()) {
                        return ResponseModel.error("您没有编辑该网格信息的权限！");
                    }
                } else {
                    return ResponseModel.error("操作失败，未查询到该网格信息！");
                }
            }
            //当前登录人ID
            ecardApiPersonArea.setCreatorId(Long.valueOf(currentUserId));
            //管户人id
            ecardApiPersonArea.setManagementPersonnelId(Long.valueOf(currentUserId));
            //登录人机构ID
            ecardApiPersonArea.setDeptId(userInfo.getDeptId());
            int id = ecardPersonAreaService.addPersonAreaInfo(ecardApiPersonArea, salt);
            return id > 0 ? ResponseModel.ok(ecardApiPersonArea.getId()) : ResponseModel.error("添加失败！");
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败，请联系管理员！");
        }
    }

    /**
     * 电子名片-条件查询登录人网格信息列表接口
     *
     * @author yuzhiguo
     * @date 2024/8/27 上午11:19
     */
    @PostMapping(value = "/getPersonAreaInfoList")
    @ApiOperation(value = "电子名片-条件查询登录人网格信息列表接口", notes = "电子名片-条件查询登录人网格信息列表接口")
    @ResponseBody
    @RsaRequest
    public ResponseModel getPersonAreaInfoList(@RequestBody String json) {
        log.info("接口请求时间-------" + DateUtils.getTime());
        try {
            String salt = getSalt();//盐值
            String currentUserId = getCurrentUserId();
            EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(currentUserId, salt);
            EcardApiEcardRegistInfo ecardApiEcardRegistInfo = registInfoService.selectEcardApiEcardRegistInfoByRegistId(Long.valueOf(currentUserId), salt);
            //查询支行机构下的所有网格
            SysDept sysDept = sysDeptService.selectThirdLevelDeptByDeptId(userInfo.getDeptId());
            JSONObject jsonObject = JSONObject.parseObject(json);
            String areaName = jsonObject.getString("areaName");//网格名称
            String type = jsonObject.getString("type");//类型 1：查询个人建档时选择的网格列表，2：查询个人列表筛选条件选择网格时展示的列表
            int pageNum = jsonObject.getInteger("pageNum");
            int pageSize = jsonObject.getInteger("pageSize");

            //当前小程序id
            SysWechatApplet wechatApplet = getAppIdByDeptId(userInfo.getDeptId());

            List<EcardApiPersonArea> personAreaList = new ArrayList<>();
            PageHelper.startPage(pageNum, pageSize);
            EcardApiPersonArea ecardApiPersonArea = new EcardApiPersonArea();
            ecardApiPersonArea.setAreaName(areaName);

            //个人列表筛选条件选择网格时展示的列表，展示本支行下所有
            if ("2".equals(type)) {
                ecardApiPersonArea.setDeptId(sysDept.getDeptId());
                personAreaList = ecardPersonAreaService.getPersonAreaInfoList(ecardApiPersonArea, salt);
                //处理每一次条网格信息中的个人数量
                for (EcardApiPersonArea personArea : personAreaList) {
                    //查询网格下的个人数量
                    EcardApiPersonInfo personInfo = new EcardApiPersonInfo();
                    personInfo.setPerAreaId(personArea.getId());
                    personInfo.setManagementPersonnelId(Long.valueOf(currentUserId));
                    personInfo.setDictDeptId(wechatApplet.getDeptId());
                    List<EcardApiPersonInfo> personList = ecardPersonInfoService.getMyPersonList(personInfo, salt);
                    personArea.setPerNumber(personList.size() + "");

                    //处理是否删除字段，用于小程序列表是否删除校验，0：是，1：否
                    EcardApiPersonInfo person = new EcardApiPersonInfo();
                    person.setPerAreaId(personArea.getId());
                    person.setDictDeptId(wechatApplet.getDeptId());
                    List<EcardApiPersonInfo> persons = ecardPersonInfoService.getPersonList(person, salt);
                    if (persons.size() > 0) {
                        personArea.setIsDelete("0"); //是否删除校验，0是，1否
                    } else {
                        personArea.setIsDelete("1"); //是否删除校验，0是，1否
                    }
                    //处理是否本人字段，用于小程序列表是否显示编辑按钮，0：是，1：否
                    //校验该网格是否在本人名下
                    if (personArea.getManagementPersonnelId().longValue() != ecardApiEcardRegistInfo.getRegistId().longValue()) {
                        personArea.setIsOneself("1");//否 不显示编辑按钮
                    } else {
                        personArea.setIsOneself("0");//是 显示编辑按钮
                    }
                }
            }

            //个人建档时选择网格，网格信息列表根据参数展示
            if ("1".equals(type)) {
                SysDept dept = sysDeptService.selectSecondLevelDeptByDeptId(userInfo.getDeptId());
                //电子名片个人信息是否共享网格 0：是，1：否
                String configValue = configService.selectConfigByKey("ecard_person_area_share", dept.getDeptId());
                if ("0".equals(configValue)) {
                    //是：查询该支行下所有的网格列表
                    ecardApiPersonArea.setDeptId(sysDept.getDeptId());
                    personAreaList = ecardPersonAreaService.getPersonAreaInfoList(ecardApiPersonArea, salt);
                } else {
                    //否：只查本人名下的网格列表
                    ecardApiPersonArea.setManagementPersonnelId(Long.valueOf(currentUserId));
                    ecardApiPersonArea.setDeptId(userInfo.getDeptId());
                    personAreaList = ecardPersonAreaService.getPersonAreaInfoList(ecardApiPersonArea, salt);
                }
            }
            PageInfo pageInfo = new PageInfo(personAreaList);
            log.info("接口返回时间-------" + DateUtils.getTime());
            return ResponseModel.okToJSONArray(pageInfo.getTotal(), personAreaList);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败，请联系管理员！");
        }
    }

    /**
     * 电子名片-条件查询全部网格信息列表接口
     *
     * @param: type 1登录人所在机构下全部，2法人机构下全部（针对网格管理岗位）
     * @param: search 查询条件
     * @author yuzhiguo
     * @date 2024/8/27 上午11:20
     */
    @PostMapping(value = "/getAllPersonAreaInfoList")
    @ApiOperation(value = "电子名片-条件查询全部网格信息列表接口", notes = "电子名片-条件查询全部网格信息列表接口")
    @ResponseBody
    @RsaRequest
    public ResponseModel getAllPersonAreaInfoList(@RequestBody String json) {
        log.info("接口请求时间-------" + DateUtils.getTime());
        try {
            String salt = getSalt();//盐值
            String currentUserId = getCurrentUserId();
            //查询客户经理信息
            EcardApiEcardRegistInfo ecardApiEcardRegistInfo = registInfoService.selectEcardApiEcardRegistInfoByRegistId(Long.valueOf(currentUserId), salt);
            JSONObject jsonObject = JSONObject.parseObject(json);
            String type = jsonObject.getString("type");
            String search = jsonObject.getString("search");//查询条件姓名和手机号拆开
            int pageNum = jsonObject.getInteger("pageNum");
            int pageSize = jsonObject.getInteger("pageSize");

            //获取当前用户的信息
            EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(currentUserId, salt);
            EcardApiPersonArea personArea = new EcardApiPersonArea();
            personArea.setAreaName(search);
            //当前小程序id
            SysWechatApplet wechatApplet = getAppIdByDeptId(userInfo.getDeptId());

            if (ecardApiEcardRegistInfo != null) {
                boolean postFlag = false;
                //多个岗位判断，只要有网格管理就传法人机构
                if (!StringUtils.objIsNullOrEmpty(userInfo.getExtendPostCode())) {
                    for (String extendPost : userInfo.getExtendPostCode().split(",")) {
                        if (wg.equals(extendPost)) {
                            postFlag = true;
                        }
                    }
                }
                if (roles.equals(ecardApiEcardRegistInfo.getRoles())) {
                    if ("1".equals(type) && !postFlag) {
                        personArea.setDeptId(userInfo.getDeptId());
                    } else if (postFlag) {
                        personArea.setDeptId(wechatApplet.getDeptId());
                    } else {
                        return ResponseModel.error("操作人岗位不正确,请联系管理员！");
                    }
                } else {
                    return ResponseModel.error("您没有查看网格列表的权限！");
                }
            } else {
                return ResponseModel.error("用户信息不存在！");
            }

            PageHelper.startPage(pageNum, pageSize);
            List<EcardApiPersonArea> personAreaList = ecardPersonAreaService.getPersonAreaInfoList(personArea, salt);
            //处理每一次条网格信息中的个人数量
            for (EcardApiPersonArea apiPersonArea : personAreaList) {
                //查询网格下的个人数量
                EcardApiPersonInfo personInfo = new EcardApiPersonInfo();
                personInfo.setPerAreaId(apiPersonArea.getId());
                personInfo.setDictDeptId(wechatApplet.getDeptId());
                List<EcardApiPersonInfo> personList = ecardPersonInfoService.getPersonList(personInfo, salt);
                apiPersonArea.setPerNumber(personList.size() + "");
            }
            PageInfo pageInfo = new PageInfo(personAreaList);
            log.info("接口返回时间-------" + DateUtils.getTime());
            return ResponseModel.okToJSONArray(pageInfo.getTotal(), personAreaList);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败，请联系管理员！");
        }
    }

    /**
     * 电子名片-条件查询网格下全部的个人列表接口
     *
     * @param: search 查询条件
     * @author yuzhiguo
     * @date 2024/8/27 上午11:38
     */
    @PostMapping(value = "/getAllPersonListByArea")
    @ApiOperation(value = "电子名片-条件查询网格下全部的个人列表接口", notes = "电子名片-条件查询网格下全部的个人列表接口")
    @ResponseBody
    @RsaRequest
    public ResponseModel getAllPersonListByArea(@RequestBody String json) {
        log.info("接口请求时间-------" + DateUtils.getTime());
        try {
            String salt = getSalt();//盐值
            EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(getCurrentUserId(), salt);
            //当前小程序id
            SysWechatApplet wechatApplet = getAppIdByDeptId(userInfo.getDeptId());
            JSONObject jsonObject = JSONObject.parseObject(json);
            String search = jsonObject.getString("search");//查询条件姓名和手机号拆开
            String phone = jsonObject.getString("phone");
            Long perAreaId = jsonObject.getLong("perAreaId");//网格id
            int pageNum = jsonObject.getInteger("pageNum");
            int pageSize = jsonObject.getInteger("pageSize");

            if (StringUtils.objIsNullOrEmpty(perAreaId)) {
                return ResponseModel.error(ResultStatus.PARAMETER_MISSING);
            }
            EcardApiPersonInfo ecardPersonInfo = new EcardApiPersonInfo();
            ecardPersonInfo.setPerCustName(search);
            ecardPersonInfo.setContactsPhone(phone);
            ecardPersonInfo.setPerAreaId(perAreaId);
            ecardPersonInfo.setDictDeptId(wechatApplet.getDeptId());

            PageHelper.startPage(pageNum, pageSize);
            List<EcardApiPersonInfo> personList = ecardPersonInfoService.getPersonList(ecardPersonInfo, salt);
            PageInfo pageInfo = new PageInfo(personList);
            log.info("接口返回时间-------" + DateUtils.getTime());
            return ResponseModel.okToJSONArray(pageInfo.getTotal(), personList);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败，请联系管理员！");
        }
    }

    /**
     * 电子名片-查询网格信息地图坐标列表接口
     *
     * @param: type 1登录人所在机构下全部，2法人机构下全部（针对网格管理岗位）
     * @author yuzhiguo
     * @date 2024/8/27 下午13:54
     */
    @PostMapping(value = "/getPersonAreaCoordinateList")
    @ApiOperation(value = "电子名片-查询网格信息地图坐标列表接口", notes = "电子名片-查询网格信息地图坐标列表接口")
    @ResponseBody
    @RsaRequest
    public ResponseModel getPersonAreaCoordinateList(@RequestBody String json) {
        try {
            String salt = getSalt();//盐值
            String currentUserId = getCurrentUserId();
            JSONObject jsonObject = JSONObject.parseObject(json);
            String type = jsonObject.getString("type");
            String coordinate = jsonObject.getString("coordinate");

            SysConfig config = new SysConfig();
            config.setConfigKey("ecard_merchant_show_distance");
            SysConfig sysConfig;

            if (StringUtils.objIsNullOrEmpty(currentUserId) || StringUtils.objIsNullOrEmpty(type)) {
                return ResponseModel.error(ResultStatus.PARAMETER_MISSING);
            }
            List<EcardApiPersonArea> personAreaList;

            if ("1".equals(type)) {
                //获取当前用户的信息
                EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(currentUserId, salt);
                personAreaList = ecardPersonAreaService.getPersonAreaCoordinateList(Long.valueOf(currentUserId), userInfo.getDeptId());
                SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(userInfo.getDeptId());
                config.setDeptId(sysDept.getDeptId());
                sysConfig = ecardApiSysConfigService.selectConfig(config);
            } else {
                EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(currentUserId, salt);
                //当前小程序id
                SysWechatApplet wechatApplet = getAppIdByDeptId(userInfo.getDeptId());
                personAreaList = ecardPersonAreaService.getAllPersonAreaCoordinateList(Long.valueOf(currentUserId), wechatApplet.getDeptId());
                SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(wechatApplet.getDeptId());
                config.setDeptId(sysDept.getDeptId());
                sysConfig = ecardApiSysConfigService.selectConfig(config);
            }

            //有经纬度就计算，没有取全部
            if (coordinate != null) {
                List<EcardApiPersonArea> list = new ArrayList<>();
                //计算是否在配置的范围之内
                //定位的网格经纬度
                String place[] = coordinate.split(",");
                for (EcardApiPersonArea personArea : personAreaList) {
                    String clockInPlace[] = personArea.getAreaCoordinate().split(",");
                    Double distance = GeoUtil.GetDistance(Double.valueOf(clockInPlace[1]), Double.valueOf(clockInPlace[0]), Double.valueOf(place[1]), Double.valueOf(place[0]));

                    BigDecimal bigDistance = new BigDecimal(distance);
                    BigDecimal bigValue = new BigDecimal(sysConfig.getConfigValue());
                    if (bigDistance.compareTo(bigValue) <= 0) {
                        list.add(personArea);
                    }
                }
                return ResponseModel.okToJSONArray(list);
            } else {
                return ResponseModel.okToJSONArray(personAreaList);
            }

        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败，请联系管理员！");
        }
    }

    /**
     * 校验网格能否新增个人接口
     *
     * @author yuzhiguo
     * @date 2024/8/27 下午2:25
     */
    @PostMapping(value = "/verifyAreaAddPer")
    @ApiOperation(value = "电子名片-校验网格能否新增个人接口", notes = "电子名片-校验网格能否新增个人接口")
    @ResponseBody
    @RsaRequest(result = false)
    public ResponseModel verifyAreaAddPer(@RequestBody String json) {
        log.info("接口请求时间-------" + DateUtils.getTime());
        try {
            String salt = getSalt();//盐值
            String currentUserId = getCurrentUserId();
            EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(currentUserId, salt);
            SysDept dept = sysDeptService.selectSecondLevelDeptByDeptId(userInfo.getDeptId());
            String configValue = configService.selectConfigByKey("ecard_person_area_share", dept.getDeptId());
            EcardApiEcardRegistInfo ecardApiEcardRegistInfo = registInfoService.selectEcardApiEcardRegistInfoByRegistId(Long.valueOf(currentUserId), salt);
            JSONObject jsonObject = JSONObject.parseObject(json);
            Long areaId = jsonObject.getLong("areaId");//网格主键id
            if (StringUtils.objIsNullOrEmpty(areaId)) {
                return ResponseModel.error(ResultStatus.PARAMETER_MISSING);
            }
            //电子名片个人信息是否共享网格 0：是，1：否
            if (!"0".equals(configValue)) {
                //校验该网格是否在本人名下
                EcardApiPersonArea personArea = ecardPersonAreaService.selectEcardPersonAreaById(areaId, salt);
                if (personArea != null) {
                    if (personArea.getManagementPersonnelId().longValue() != ecardApiEcardRegistInfo.getRegistId().longValue()) {
                        return ResponseModel.error("您没有该网格新增个人的权限！");
                    }
                }
            }
            log.info("接口返回时间-------" + DateUtils.getTime());
            return ResponseModel.ok();
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败，请联系管理员！");
        }
    }

    /**
     * 计算个人坐标是否在选择网格的配置的范围内
     *
     * @author yuzhiguo
     * @date 2024/8/27 下午2:37
     */
    @PostMapping(value = "/computePerAndAreaRadius")
    @ApiOperation(value = "电子名片-计算个人坐标是否在选择网格的配置的范围内", notes = "电子名片-计算个人坐标是否在选择网格的配置的范围内")
    @ResponseBody
    @RsaRequest(result = false)
    public ResponseModel computePerAndAreaRadius(@RequestBody String json) {
        String salt = getSalt();//盐值
        JSONObject jsonObject = JSONObject.parseObject(json);
        String coordinate = jsonObject.getString("coordinate");//个人坐标
        Long id = jsonObject.getLong("id");//网格id
        if (StringUtils.objIsNullOrEmpty(coordinate) || StringUtils.objIsNullOrEmpty(id)) {
            return ResponseModel.error(ResultStatus.PARAMETER_MISSING);
        }
        String currentUserId = getCurrentUserId();
        EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(currentUserId, salt);

        EcardApiPersonArea personArea = ecardPersonAreaService.selectEcardPersonAreaById(id, salt);
        //网格的经纬度
        String clockInPlace[] = personArea.getAreaCoordinate().split(",");
        //定位的个人经纬度
        String place[] = coordinate.split(",");
        Double distance = GeoUtil.GetDistance(Double.valueOf(clockInPlace[1]), Double.valueOf(clockInPlace[0]), Double.valueOf(place[1]), Double.valueOf(place[0]));

        SysConfig config = new SysConfig();
        config.setConfigKey("ecard_person_area_distance");
        SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(userInfo.getDeptId());
        config.setDeptId(sysDept.getDeptId());
        SysConfig sysConfig = ecardApiSysConfigService.selectConfig(config);

        BigDecimal bigDistance = new BigDecimal(distance);
        BigDecimal bigValue = new BigDecimal(sysConfig.getConfigValue());
        System.out.println("个人和网格之间的距离为========================= " + bigDistance);
        if (bigDistance.compareTo(bigValue) > 0) {
            return ResponseModel.error("所选个人位置超出系统配置的距离，请重新选择！");
        } else {
            return ResponseModel.ok();
        }

    }

    /**
     * 电子名片-保存网格信息转交记录接口
     *
     * @author yuzhiguo
     * @date 2024/8/27 下午3:00
     */
    @PostMapping(value = "/addAreaTransferInfo")
    @ApiOperation(value = "电子名片-保存网格信息转交记录接口", notes = "电子名片-保存网格信息转交记录接口")
    @ResponseBody
    @RsaRequest(result = false)
    public ResponseModel addAreaTransferInfo(@RequestBody EcardApiPersonAreaHandover ecardPersonAreaHandover) {
        try {
            String salt = getSalt();//盐值
            String currentUserId = getCurrentUserId();
            EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(currentUserId, salt);
            EcardApiEcardRegistInfo ecardApiEcardRegistInfo = registInfoService.selectEcardApiEcardRegistInfoByRegistId(Long.valueOf(currentUserId), salt);
            //转交时校验该网格是否在本人名下
            EcardApiPersonArea personArea = ecardPersonAreaService.selectEcardPersonAreaById(ecardPersonAreaHandover.getAreaId(), salt);
            if (personArea != null) {
                if (personArea.getManagementPersonnelId().longValue() != ecardApiEcardRegistInfo.getRegistId().longValue()) {
                    return ResponseModel.error("您没有转交该网格信息的权限！");
                }
            }
            //当前登录人userId
            ecardPersonAreaHandover.setTransferId(Long.valueOf(currentUserId));
            ecardPersonAreaHandover.setTransferDeptId(userInfo.getDeptId());
            int updateCount = ecardPersonAreaService.addAreaTransferInfo(ecardPersonAreaHandover);
            return updateCount > 0 ? ResponseModel.ok() : ResponseModel.error("转交失败！");
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败，请联系管理员！");
        }
    }

    /**
     * 电子名片-查询网格信息详情接口
     *
     * @author yuzhiguo
     * @date 2024/8/27 下午3:20
     */
    @PostMapping("/getPersonAreaInfo")
    @ApiOperation(value = "电子名片-查询网格信息详情接口", notes = "电子名片-查询网格信息详情接口")
    @ResponseBody
    @RsaRequest
    public ResponseModel getPersonAreaInfo(@RequestBody String json) {
        try {
            String salt = getSalt();//盐值
            JSONObject jsonObject = JSONObject.parseObject(json);
            Long id = jsonObject.getLong("id");
            if (StringUtils.objIsNullOrEmpty(id)) {
                return ResponseModel.error(ResultStatus.PARAMETER_MISSING);
            }
            EcardApiPersonArea personArea = ecardPersonAreaService.selectEcardPersonAreaById(id, salt);
            return ResponseModel.okToJSON(personArea);

        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败，请联系管理员！");
        }
    }

    /**
     * 电子名片-根据ID修改网格信息状态
     *
     * @author yuzhiguo
     * @date 2024/8/27 下午3:26
     */
    @PostMapping(value = "/changePersonAreaStatus")
    @ApiOperation(value = "电子名片-根据ID修改网格信息状态", notes = "电子名片-根据ID修改网格信息状态")
    @ResponseBody
    @RsaRequest(result = false)
    public ResponseModel changePersonAreaStatus(@RequestBody String json) {
        try {
            String salt = getSalt();//盐值
            JSONObject jsonObject = JSONObject.parseObject(json);
            Long id = jsonObject.getLong("id");
            String currentUserId = getCurrentUserId();
            EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(currentUserId, salt);
            EcardApiEcardRegistInfo ecardApiEcardRegistInfo = registInfoService.selectEcardApiEcardRegistInfoByRegistId(Long.valueOf(currentUserId), salt);
            if (StringUtils.objIsNullOrEmpty(id)) {
                return ResponseModel.error(ResultStatus.PARAMETER_MISSING);
            }
            //当前小程序id
            SysWechatApplet wechatApplet = getAppIdByDeptId(userInfo.getDeptId());
            //删除时校验该网格是否在本人名下
            EcardApiPersonArea personArea = ecardPersonAreaService.selectEcardPersonAreaById(id, salt);
            if (personArea != null) {
                if (personArea.getManagementPersonnelId().longValue() != ecardApiEcardRegistInfo.getRegistId().longValue()) {
                    return ResponseModel.error("您没有删除该网格信息的权限！");
                }
            }
            EcardApiPersonInfo personInfo = new EcardApiPersonInfo();
            personInfo.setPerAreaId(id);
            personInfo.setDictDeptId(wechatApplet.getDeptId());
            List<EcardApiPersonInfo> personList = ecardPersonInfoService.getPersonList(personInfo, salt);
            if (personList.size() > 0) {
                return ResponseModel.error("该网格下存在个人信息，无法删除！");
            }
            int updateCount = ecardPersonAreaService.changePersonAreaStatus(id);
            return updateCount > 0 ? ResponseModel.ok() : ResponseModel.error("删除失败！");
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败，请联系管理员！");
        }
    }

//-----------------------------------------------------------------以下为统计接口--------------------------------------------------------------------

    /**
     * 电子名片-我的个人建档拜访数量统计接口
     *
     * @author yuzhiguo
     * @date 2024/11/5 上午8:48
     */
    @PostMapping(value = "/getPerFilingVisitsStatistics")
    @ApiOperation(value = "电子名片-我的个人建档拜访数量统计接口", notes = "电子名片-我的个人建档拜访数量统计接口")
    @ResponseBody
    @RsaRequest()
    public ResponseModel getPerFilingVisitsStatistics(@RequestBody String json) {
        try {
            String salt = getSalt();//盐值
            JSONObject jsonParam = JSONObject.parseObject(json);
            String perType = jsonParam.getString("perType");//个人客户类型，1：农户，2：新市民
            //获取当前用户的信息
            EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(getCurrentUserId(), salt);
            JSONObject jsonPerObject = ecardBranchPersonChartService.getPerFilingVisitsStatistics(userInfo.getUserId(), perType);//个人建档相关
            JSONObject jsonAreaObject = ecardBranchPersonChartService.getPerAreaFilings(userInfo.getUserId());//网格建档相关
            EcardBranchEcardChart ecardChart = new EcardBranchEcardChart();
            ecardChart.setMerchantNum(jsonPerObject.getLong("perPerFilingNums"));//个人本人累计建档总数
            ecardChart.setVisitSum(jsonPerObject.getLong("perPerVisitNums"));//个人本人累计拜访总数
            ecardChart.setDoTaskSum(jsonPerObject.getLong("perPerDoVisitNums"));//个人本人超时拜访总数
            ecardChart.setUndoTaskSum(jsonPerObject.getLong("perPerUnDoVisitNums"));//个人本人超时未拜访总数
            ecardChart.setMerchantMonthNum(jsonPerObject.getLong("perPerFilingThisMonthNums"));//个人本人本月新增建档总数
            ecardChart.setMerchantLastMonthNum(jsonPerObject.getLong("perPerFilingCompareNums"));//个人本人建档较上月对比
            ecardChart.setVisitMouthSum(jsonPerObject.getLong("perPerVisitThisMonthNums"));//个人本人本月新增拜访总数
            ecardChart.setVisitLastMouthNum(jsonPerObject.getLong("perPerVisitCompareNums"));//个人本人拜访较上月对比
            if (ecardChart.getMerchantLastMonthNum() >= 0){
                ecardChart.setCompareMerchantType("1");//较上月建档类型（1：上升，2：下降）
            } else {
                ecardChart.setCompareMerchantType("2");//较上月建档类型（1：上升，2：下降）
            }
            if (ecardChart.getVisitLastMouthNum() >= 0){
                ecardChart.setCompareVisitType("1");//较上月拜访类型（1：上升，2：下降）
            } else {
                ecardChart.setCompareVisitType("2");//较上月拜访类型（1：上升，2：下降）
            }
            ecardChart.setPerAreaFilingNums(jsonAreaObject.getLong("perAreaFilingNums"));//个人本人网格累计建档总数
            ecardChart.setPerAreaFilingThisMonthNums(jsonAreaObject.getLong("perAreaFilingThisMonthNums"));//个人本人本月网格新增建档总数
            ecardChart.setPerAreaFilingCompareNums(jsonAreaObject.getLong("perAreaFilingCompareNums"));//个人本人网格建档较上月对比
            if (ecardChart.getPerAreaFilingCompareNums() >= 0){
                ecardChart.setPerAreaFilingType("1");//较上月网格建档类型（1：上升，2：下降）
            } else {
                ecardChart.setPerAreaFilingType("2");//较上月网格建档类型（1：上升，2：下降）
            }
            return ResponseModel.okToJSON(ecardChart);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败,请联系管理员!");
        }
    }

    /**
     * 电子名片-全行个人建档拜访数量统计接口
     *
     * @author yuzhiguo
     * @date 2024/11/5 上午9:31
     */
    @PostMapping(value = "/getPerAllFilingVisitsNums")
    @ApiOperation(value = "电子名片-全行个人建档拜访数量统计接口", notes = "电子名片-全行个人建档拜访数量统计接口")
    @ResponseBody
    @RsaRequest()
    public ResponseModel getPerAllFilingVisitsNums(@RequestBody String json) {
        try {
            String salt = getSalt();//盐值
            JSONObject jsonParam = JSONObject.parseObject(json);
            String perType = jsonParam.getString("perType");//个人客户类型，1：农户，2：新市民
            //获取当前用户的信息
            EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(getCurrentUserId(), salt);
            JSONObject jsonPerObject = ecardBranchPersonChartService.getPerAllFilingVisitsNums(userInfo.getDeptId(), perType);
            JSONObject jsonAreaObject = ecardBranchPersonChartService.getPerAllAreaFilings(userInfo.getDeptId());
            EcardBranchEcardChart ecardChart = new EcardBranchEcardChart();
            ecardChart.setMerchantNum(jsonPerObject.getLong("perFilingNums"));//全行个人建档总数
            ecardChart.setFilingLastMonthNums(jsonPerObject.getLong("perFilingLastMonthNums"));//上月新增个人建档总数
            ecardChart.setMerchantMonthNum(jsonPerObject.getLong("perFilingThisMonthNums"));//本月新增个人建档总数
            ecardChart.setMerchantLastMonthNum(jsonPerObject.getLong("perFilingCompareNums"));//个人建档较上月对比
            ecardChart.setVisitSum(jsonPerObject.getLong("perVisitNums"));//个人拜访总次数
            ecardChart.setVisitNewLastMonthNums(jsonPerObject.getLong("perVisitLastMonthNums"));//上月新增个人拜访总数
            ecardChart.setVisitMouthSum(jsonPerObject.getLong("perVisitThisMonthNums"));//本月新增个人拜访总数
            ecardChart.setVisitLastMouthNum(jsonPerObject.getLong("perVisitCompareNums"));//个人拜访较上月对比
            if (ecardChart.getMerchantLastMonthNum() >= 0){
                ecardChart.setCompareMerchantType("1");//较上月个人建档类型（1：上升，2：下降）
            } else {
                ecardChart.setCompareMerchantType("2");//较上月个人建档类型（1：上升，2：下降）
            }
            if (ecardChart.getVisitLastMouthNum() >= 0){
                ecardChart.setCompareVisitType("1");//较上月个人拜访类型（1：上升，2：下降）
            } else {
                ecardChart.setCompareVisitType("2");//较上月个人拜访类型（1：上升，2：下降）
            }
            ecardChart.setPerAreaFilingNums(jsonAreaObject.getLong("perAreaFilingNums"));//全行网格建档总数
            ecardChart.setPerAreaFilingLastMonthNums(jsonAreaObject.getLong("perAreaFilingLastMonthNums"));//上月新增网格建档总数
            ecardChart.setPerAreaFilingThisMonthNums(jsonAreaObject.getLong("perAreaFilingThisMonthNums"));//本月新增网格建档总数
            ecardChart.setPerAreaFilingCompareNums(jsonAreaObject.getLong("perAreaFilingCompareNums"));//网格建档较上月对比
            if (ecardChart.getPerAreaFilingCompareNums() >= 0){
                ecardChart.setPerAreaFilingType("1");//较上月网格建档类型（1：上升，2：下降）
            } else {
                ecardChart.setPerAreaFilingType("2");//较上月网格建档类型（1：上升，2：下降）
            }
            return ResponseModel.okToJSON(ecardChart);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败,请联系管理员!");
        }
    }

    /**
     * 电子名片-个人超时拜访数量统计接口
     *
     * @author yuzhiguo
     * @date 2024/11/5 上午9:41
     */
    @PostMapping(value = "/getPerTimeoutVisitsNums")
    @ApiOperation(value = "电子名片-个人超时拜访数量统计接口", notes = "电子名片-个人超时拜访数量统计接口")
    @ResponseBody
    @RsaRequest()
    public ResponseModel getPerTimeoutVisitsNums(@RequestBody String json) {
        try {
            String salt = getSalt();//盐值
            JSONObject jsonParam = JSONObject.parseObject(json);
            String perType = jsonParam.getString("perType");//个人客户类型，1：农户，2：新市民
            //获取当前用户的信息
            EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(getCurrentUserId(), salt);
            JSONObject jsonObject = ecardBranchPersonChartService.getPerTimeoutVisitsNums(userInfo.getDeptId(), perType);
            EcardBranchEcardChart ecardChart = new EcardBranchEcardChart();
            ecardChart.setDoTaskSum(jsonObject.getLong("perDoTaskNums"));//个人超时拜访总数
            ecardChart.setDoTaskLastMonthNums(jsonObject.getLong("perDoTaskLastMonthNums"));//上月新增个人超时拜访总数
            ecardChart.setDoTaskThisMonthNums(jsonObject.getLong("perDoTaskThisMonthNums"));//本月新增个人超时拜访总数
            ecardChart.setDoTaskCompareNums(jsonObject.getLong("perDoTaskCompareNums"));//个人超时拜访较上月对比
            ecardChart.setUndoTaskSum(jsonObject.getLong("perUndoTaskNums"));//个人超时未拜访总次数
            ecardChart.setUndoTaskLastMonthNums(jsonObject.getLong("perUndoTaskLastMonthNums"));//上月新增个人超时未拜访总数
            ecardChart.setUndoTaskThisMonthNums(jsonObject.getLong("perUndoTaskThisMonthNums"));//本月新增个人超时未拜访总数
            ecardChart.setUndoTaskCompareNums(jsonObject.getLong("perUndoTaskCompareNums"));//个人超时未拜访较上月对比
            if (ecardChart.getDoTaskCompareNums() >= 0){
                ecardChart.setCompareDoType("1");//较上月超时拜访类型（1：上升，2：下降）
            } else {
                ecardChart.setCompareDoType("2");//较上月超时拜访类型（1：上升，2：下降）
            }
            if (ecardChart.getUndoTaskCompareNums() >= 0){
                ecardChart.setCompareUndoType("1");//较上月超时未拜访类型（1：上升，2：下降）
            } else {
                ecardChart.setCompareUndoType("2");//较上月超时未拜访类型（1：上升，2：下降）
            }
            return ResponseModel.okToJSON(ecardChart);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败,请联系管理员!");
        }
    }

    /**
     * 电子名片-查询个人建档折线图数据接口
     *
     * @author yuzhiguo
     * @date 2024/11/5 上午9:45
     */
    @PostMapping(value = "/getPerFilingLineChart")
    @ApiOperation(value = "电子名片-查询个人建档折线图数据接口", notes = "电子名片-查询个人建档折线图数据接口")
    @ResponseBody
    @RsaRequest()
    public ResponseModel getPerFilingLineChart(@RequestBody String json) {
        try {
            String salt = getSalt();//盐值
            JSONObject jsonParam = JSONObject.parseObject(json);
            String perType = jsonParam.getString("perType");//个人客户类型，1：农户，2：新市民
            //获取当前用户的信息
            EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(getCurrentUserId(), salt);
            List<EcardBranchEcardChart> ecardBranchEcardChartList = ecardBranchPersonChartService.getPerFilingLineChartByApplet(userInfo.getDeptId(), perType);
            return ResponseModel.okToJSONArray(ecardBranchEcardChartList);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败,请联系管理员!");
        }
    }

    /**
     * 电子名片-查询个人拜访柱状图数据接口
     *
     * @author yuzhiguo
     * @date 2024/11/5 上午9:49
     */
    @PostMapping(value = "/getPerVisitLineChart")
    @ApiOperation(value = "电子名片-查询个人拜访柱状图数据接口", notes = "电子名片-查询个人拜访柱状图数据接口")
    @ResponseBody
    @RsaRequest()
    public ResponseModel getPerVisitLineChart(@RequestBody String json) {
        try {
            String salt = getSalt();//盐值
            JSONObject jsonParam = JSONObject.parseObject(json);
            String perType = jsonParam.getString("perType");//个人客户类型，1：农户，2：新市民
            //获取当前用户的信息
            EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(getCurrentUserId(), salt);
            List<EcardBranchEcardChart> ecardBranchEcardChartList = ecardBranchPersonChartService.getPerVisitLineChartByApplet(userInfo.getDeptId(), perType);
            return ResponseModel.okToJSONArray(ecardBranchEcardChartList);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败,请联系管理员!");
        }
    }

    /**
     * 电子名片-各机构个人建档排名接口
     *
     * @author yuzhiguo
     * @date 2024/11/5 上午9:53
     */
    @PostMapping(value = "/getPerDeptFilingList")
    @ApiOperation(value = "电子名片-各机构个人建档排名接口", notes = "电子名片-各机构个人建档排名接口")
    @ResponseBody
    @RsaRequest(param = true, result = true)
    public ResponseModel getPerDeptFilingList(@RequestBody String json) {
        try {
            String salt = getSalt();//盐值
            //获取当前用户的信息
            EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(getCurrentUserId(), salt);
            JSONObject jsonObject = JSONObject.parseObject(json);
            int pageNum = jsonObject.getInteger("pageNum");
            int pageSize = jsonObject.getInteger("pageSize");
            String perType = jsonObject.getString("perType");//个人客户类型，1：农户，2：新市民
            if (StringUtils.objIsNullOrEmpty(pageNum) || StringUtils.objIsNullOrEmpty(pageSize)) {
                return ResponseModel.error(ResultStatus.PARAMETER_MISSING);
            }
            EcardBranchDeptFiling ecardBranchDeptFiling = new EcardBranchDeptFiling();
            SysDept dept = sysDeptService.selectSecondLevelDeptByDeptId(userInfo.getDeptId());
            //是否只查业务条线（支行和分理处，不含总行各部室），0：是，1：否
            String ecardMerchantIsdept = sysConfigService.selectConfigByKey("ecard_merchant_isdept", dept.getDeptId());
            if ("0".equals(ecardMerchantIsdept)){
                ecardBranchDeptFiling.setIsServiceLine(ecardMerchantIsdept);//是
            }
            SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(userInfo.getDeptId());//获取县级机构
            ecardBranchDeptFiling.setDeptId(sysDept.getDeptId());
            ecardBranchDeptFiling.setPerType(perType);//个人客户类型，1：农户，2：新市民
            PageHelper.startPage(pageNum, pageSize);
            List<EcardBranchDeptFiling> ecardBranchDeptFilingList = ecardBranchPersonChartService.getPerDeptFilingList(ecardBranchDeptFiling);
            for (EcardBranchDeptFiling branchDeptFiling : ecardBranchDeptFilingList) {
                if (branchDeptFiling.getMerchantCompareNum() >= 0){
                    branchDeptFiling.setCompareType("1");//较上月类型（1：上升，2：下降）
                } else {
                    branchDeptFiling.setCompareType("2");//较上月类型（1：上升，2：下降）
                }
            }
            PageInfo pageInfo = new PageInfo(ecardBranchDeptFilingList);
            return ResponseModel.okToJSONArray(pageInfo.getTotal(), ecardBranchDeptFilingList);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败,请联系管理员!");
        }
    }

    /**
     * 电子名片-营销人员个人建档排名接口
     *
     * @author yuzhiguo
     * @date 2024/11/5 上午9:55
     */
    @PostMapping(value = "/getPerUserFilingList")
    @ApiOperation(value = "电子名片-营销人员个人建档排名接口", notes = "电子名片-营销人员个人建档排名接口")
    @ResponseBody
    @RsaRequest(param = true, result = true)
    public ResponseModel getPerUserFilingList(@RequestBody String json) {
        try {
            String salt = getSalt();//盐值
            //获取当前用户的信息
            EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(getCurrentUserId(), salt);
            JSONObject jsonObject = JSONObject.parseObject(json);
            int pageNum = jsonObject.getInteger("pageNum");
            int pageSize = jsonObject.getInteger("pageSize");
            String perType = jsonObject.getString("perType");//个人客户类型，1：农户，2：新市民
            Long appQueryDeptId = jsonObject.getLong("appQueryDeptId");//小程序传参查询用的deptId
            String userName = jsonObject.getString("userName");//小程序传参查询用的营销人员姓名
            if (StringUtils.objIsNullOrEmpty(pageNum) || StringUtils.objIsNullOrEmpty(pageSize)) {
                return ResponseModel.error(ResultStatus.PARAMETER_MISSING);
            }
            EcardBranchUserFiling ecardBranchUserFiling = new EcardBranchUserFiling();
            SysDept dept = sysDeptService.selectSecondLevelDeptByDeptId(userInfo.getDeptId());
            //是否只查业务条线（支行和分理处，不含总行各部室），0：是，1：否
            String ecardMerchantIsdept = sysConfigService.selectConfigByKey("ecard_merchant_isdept", dept.getDeptId());
            if ("0".equals(ecardMerchantIsdept)){
                ecardBranchUserFiling.setIsServiceLine(ecardMerchantIsdept);//是
            }
            SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(userInfo.getDeptId());//获取县级机构
            ecardBranchUserFiling.setDeptId(sysDept.getDeptId());
            ecardBranchUserFiling.setAppQueryDeptId(appQueryDeptId);
            ecardBranchUserFiling.setUserName(userName);
            ecardBranchUserFiling.setPerType(perType);//个人客户类型，1：农户，2：新市民
            PageHelper.startPage(pageNum, pageSize);
            List<EcardBranchUserFiling> ecardBranchUserFilingList = ecardBranchPersonChartService.getPerUserFilingList(ecardBranchUserFiling);
            PageInfo pageInfo = new PageInfo(ecardBranchUserFilingList);
            return ResponseModel.okToJSONArray(pageInfo.getTotal(), ecardBranchUserFilingList);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败,请联系管理员!");
        }
    }

    /**
     * 电子名片-机构个人拜访次数排名接口
     *
     * @author yuzhiguo
     * @date 2024/11/5 上午9:58
     */
    @PostMapping(value = "/getPerDeptVisitsList")
    @ApiOperation(value = "电子名片-机构个人拜访次数排名接口", notes = "电子名片-机构个人拜访次数排名接口")
    @ResponseBody
    @RsaRequest(param = true, result = true)
    public ResponseModel getPerDeptVisitsList(@RequestBody String json) {
        try {
            String salt = getSalt();//盐值
            //获取当前用户的信息
            EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(getCurrentUserId(), salt);
            JSONObject jsonObject = JSONObject.parseObject(json);
            int pageNum = jsonObject.getInteger("pageNum");
            int pageSize = jsonObject.getInteger("pageSize");
            String perType = jsonObject.getString("perType");//个人客户类型，1：农户，2：新市民
            if (StringUtils.objIsNullOrEmpty(pageNum) || StringUtils.objIsNullOrEmpty(pageSize)) {
                return ResponseModel.error(ResultStatus.PARAMETER_MISSING);
            }
            EcardBranchDeptVisit ecardBranchDeptVisit = new EcardBranchDeptVisit();
            SysDept dept = sysDeptService.selectSecondLevelDeptByDeptId(userInfo.getDeptId());
            //是否只查业务条线（支行和分理处，不含总行各部室），0：是，1：否
            String ecardMerchantIsdept = sysConfigService.selectConfigByKey("ecard_merchant_isdept", dept.getDeptId());
            if ("0".equals(ecardMerchantIsdept)){
                ecardBranchDeptVisit.setIsServiceLine(ecardMerchantIsdept);//是
            }
            SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(userInfo.getDeptId());//获取县级机构
            ecardBranchDeptVisit.setDeptId(sysDept.getDeptId());
            ecardBranchDeptVisit.setPerType(perType);//个人客户类型，1：农户，2：新市民
            PageHelper.startPage(pageNum, pageSize);
            List<EcardBranchDeptVisit> ecardBranchDeptVisitList = ecardBranchPersonChartService.getPerDeptVisitsList(ecardBranchDeptVisit);
            for (EcardBranchDeptVisit branchDeptVisit : ecardBranchDeptVisitList) {
                if (branchDeptVisit.getVisitCompareNum() >= 0){
                    branchDeptVisit.setCompareType("1");//较上月类型（1：上升，2：下降）
                } else {
                    branchDeptVisit.setCompareType("2");//较上月类型（1：上升，2：下降）
                }
            }
            PageInfo pageInfo = new PageInfo(ecardBranchDeptVisitList);
            return ResponseModel.okToJSONArray(pageInfo.getTotal(), ecardBranchDeptVisitList);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败,请联系管理员!");
        }
    }

    /**
     * 电子名片-营销人员个人拜访次数排名
     *
     * @author yuzhiguo
     * @date 2024/11/5 上午10:00
     */
    @PostMapping(value = "/getPerUserVisitsList")
    @ApiOperation(value = "电子名片-营销人员个人拜访次数排名", notes = "电子名片-营销人员个人拜访次数排名")
    @ResponseBody
    @RsaRequest(param = true, result = true)
    public ResponseModel getPerUserVisitsList(@RequestBody String json) {
        try {
            String salt = getSalt();//盐值
            //获取当前用户的信息
            EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(getCurrentUserId(), salt);
            JSONObject jsonObject = JSONObject.parseObject(json);
            int pageNum = jsonObject.getInteger("pageNum");
            int pageSize = jsonObject.getInteger("pageSize");
            Long appQueryDeptId = jsonObject.getLong("appQueryDeptId");//小程序传参查询用的deptId
            String userName = jsonObject.getString("userName");//小程序传参查询用的营销人员姓名
            String perType = jsonObject.getString("perType");//个人客户类型，1：农户，2：新市民
            if (StringUtils.objIsNullOrEmpty(pageNum) || StringUtils.objIsNullOrEmpty(pageSize)) {
                return ResponseModel.error(ResultStatus.PARAMETER_MISSING);
            }
            EcardBranchUserVisit ecardBranchUserVisit = new EcardBranchUserVisit();
            SysDept dept = sysDeptService.selectSecondLevelDeptByDeptId(userInfo.getDeptId());
            //是否只查业务条线（支行和分理处，不含总行各部室），0：是，1：否
            String ecardMerchantIsdept = sysConfigService.selectConfigByKey("ecard_merchant_isdept", dept.getDeptId());
            if ("0".equals(ecardMerchantIsdept)){
                ecardBranchUserVisit.setIsServiceLine(ecardMerchantIsdept);//是
            }
            SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(userInfo.getDeptId());//获取县级机构
            ecardBranchUserVisit.setDeptId(sysDept.getDeptId());
            ecardBranchUserVisit.setAppQueryDeptId(appQueryDeptId);
            ecardBranchUserVisit.setUserName(userName);
            ecardBranchUserVisit.setPerType(perType);//个人客户类型，1：农户，2：新市民
            PageHelper.startPage(pageNum, pageSize);
            List<EcardBranchUserVisit> ecardBranchUserVisitList = ecardBranchPersonChartService.getPerUserVisitsList(ecardBranchUserVisit);
            PageInfo pageInfo = new PageInfo(ecardBranchUserVisitList);
            return ResponseModel.okToJSONArray(pageInfo.getTotal(), ecardBranchUserVisitList);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败,请联系管理员!");
        }
    }

    /**
     * 电子名片-机构个人超时拜访次数排名接口
     *
     * @author yuzhiguo
     * @date 2024/11/5 上午10:03
     */
    @PostMapping(value = "/getPerDeptDoTaskList")
    @ApiOperation(value = "电子名片-机构个人超时拜访次数排名接口", notes = "电子名片-机构个人超时拜访次数排名接口")
    @ResponseBody
    @RsaRequest(param = true, result = true)
    public ResponseModel getPerDeptDoTaskList(@RequestBody String json) {
        try {
            String salt = getSalt();//盐值
            //获取当前用户的信息
            EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(getCurrentUserId(), salt);
            JSONObject jsonObject = JSONObject.parseObject(json);
            int pageNum = jsonObject.getInteger("pageNum");
            int pageSize = jsonObject.getInteger("pageSize");
            String perType = jsonObject.getString("perType");//个人客户类型，1：农户，2：新市民
            if (StringUtils.objIsNullOrEmpty(pageNum) || StringUtils.objIsNullOrEmpty(pageSize)) {
                return ResponseModel.error(ResultStatus.PARAMETER_MISSING);
            }
            EcardBranchDeptDoTask ecardBranchDeptDoTask = new EcardBranchDeptDoTask();
            SysDept dept = sysDeptService.selectSecondLevelDeptByDeptId(userInfo.getDeptId());
            //是否只查业务条线（支行和分理处，不含总行各部室），0：是，1：否
            String ecardMerchantIsdept = sysConfigService.selectConfigByKey("ecard_merchant_isdept", dept.getDeptId());
            if ("0".equals(ecardMerchantIsdept)){
                ecardBranchDeptDoTask.setIsServiceLine(ecardMerchantIsdept);//是
            }
            SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(userInfo.getDeptId());//获取县级机构
            ecardBranchDeptDoTask.setDeptId(sysDept.getDeptId());
            ecardBranchDeptDoTask.setPerType(perType);//个人客户类型，1：农户，2：新市民
            PageHelper.startPage(pageNum, pageSize);
            List<EcardBranchDeptDoTask> ecardBranchDeptDoTaskList = ecardBranchPersonChartService.getPerDeptDoTaskList(ecardBranchDeptDoTask);
            PageInfo pageInfo = new PageInfo(ecardBranchDeptDoTaskList);
            return ResponseModel.okToJSONArray(pageInfo.getTotal(), ecardBranchDeptDoTaskList);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败,请联系管理员!");
        }
    }

    /**
     * 电子名片-机构个人超时未拜访次数排名接口
     *
     * @author yuzhiguo
     * @date 2024/11/5 上午10:05
     */
    @PostMapping(value = "/getPerDeptUnDoTaskList")
    @ApiOperation(value = "电子名片-机构个人超时未拜访次数排名接口", notes = "电子名片-机构个人超时未拜访次数排名接口")
    @ResponseBody
    @RsaRequest(param = true, result = true)
    public ResponseModel getPerDeptUnDoTaskList(@RequestBody String json) {
        try {
            String salt = getSalt();//盐值
            //获取当前用户的信息
            EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(getCurrentUserId(), salt);
            JSONObject jsonObject = JSONObject.parseObject(json);
            int pageNum = jsonObject.getInteger("pageNum");
            int pageSize = jsonObject.getInteger("pageSize");
            String perType = jsonObject.getString("perType");//个人客户类型，1：农户，2：新市民
            if (StringUtils.objIsNullOrEmpty(pageNum) || StringUtils.objIsNullOrEmpty(pageSize)) {
                return ResponseModel.error(ResultStatus.PARAMETER_MISSING);
            }
            EcardBranchDeptUndoTask ecardBranchDeptUndoTask = new EcardBranchDeptUndoTask();
            SysDept dept = sysDeptService.selectSecondLevelDeptByDeptId(userInfo.getDeptId());
            //是否只查业务条线（支行和分理处，不含总行各部室），0：是，1：否
            String ecardMerchantIsdept = sysConfigService.selectConfigByKey("ecard_merchant_isdept", dept.getDeptId());
            if ("0".equals(ecardMerchantIsdept)){
                ecardBranchDeptUndoTask.setIsServiceLine(ecardMerchantIsdept);//是
            }
            SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(userInfo.getDeptId());//获取县级机构
            ecardBranchDeptUndoTask.setDeptId(sysDept.getDeptId());
            ecardBranchDeptUndoTask.setPerType(perType);//个人客户类型，1：农户，2：新市民
            PageHelper.startPage(pageNum, pageSize);
            List<EcardBranchDeptUndoTask> ecardBranchDeptUndoTaskList = ecardBranchPersonChartService.getPerDeptUnDoTaskList(ecardBranchDeptUndoTask);
            PageInfo pageInfo = new PageInfo(ecardBranchDeptUndoTaskList);
            return ResponseModel.okToJSONArray(pageInfo.getTotal(), ecardBranchDeptUndoTaskList);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败,请联系管理员!");
        }
    }

    /**
     * 电子名片-营销人员个人超时拜访次数排名接口
     *
     * @author yuzhiguo
     * @date 2024/11/5 上午10:07
     */
    @PostMapping(value = "/getPerUserDoTaskList")
    @ApiOperation(value = "电子名片-营销人员个人超时拜访次数排名接口", notes = "电子名片-营销人员个人超时拜访次数排名接口")
    @ResponseBody
    @RsaRequest(param = true, result = true)
    public ResponseModel getPerUserDoTaskList(@RequestBody String json) {
        try {
            String salt = getSalt();//盐值
            //获取当前用户的信息
            EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(getCurrentUserId(), salt);
            JSONObject jsonObject = JSONObject.parseObject(json);
            int pageNum = jsonObject.getInteger("pageNum");
            int pageSize = jsonObject.getInteger("pageSize");
            Long appQueryDeptId = jsonObject.getLong("appQueryDeptId");//小程序传参查询用的deptId
            String userName = jsonObject.getString("userName");//小程序传参查询用的营销人员姓名
            String perType = jsonObject.getString("perType");//个人客户类型，1：农户，2：新市民
            if (StringUtils.objIsNullOrEmpty(pageNum) || StringUtils.objIsNullOrEmpty(pageSize)) {
                return ResponseModel.error(ResultStatus.PARAMETER_MISSING);
            }
            EcardBranchUserDoTask ecardBranchUserDoTask = new EcardBranchUserDoTask();
            SysDept dept = sysDeptService.selectSecondLevelDeptByDeptId(userInfo.getDeptId());
            //是否只查业务条线（支行和分理处，不含总行各部室），0：是，1：否
            String ecardMerchantIsdept = sysConfigService.selectConfigByKey("ecard_merchant_isdept", dept.getDeptId());
            if ("0".equals(ecardMerchantIsdept)){
                ecardBranchUserDoTask.setIsServiceLine(ecardMerchantIsdept);//是
            }
            SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(userInfo.getDeptId());//获取县级机构
            ecardBranchUserDoTask.setDeptId(sysDept.getDeptId());
            ecardBranchUserDoTask.setAppQueryDeptId(appQueryDeptId);
            ecardBranchUserDoTask.setUserName(userName);
            ecardBranchUserDoTask.setPerType(perType);//个人客户类型，1：农户，2：新市民
            PageHelper.startPage(pageNum, pageSize);
            List<EcardBranchUserDoTask> ecardBranchUserDoTaskList = ecardBranchPersonChartService.getPerUserDoTaskList(ecardBranchUserDoTask);
            PageInfo pageInfo = new PageInfo(ecardBranchUserDoTaskList);
            return ResponseModel.okToJSONArray(pageInfo.getTotal(), ecardBranchUserDoTaskList);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败,请联系管理员!");
        }
    }

    /**
     * 电子名片-营销人员个人超时未拜访次数排名接口
     *
     * @author yuzhiguo
     * @date 2024/11/5 上午10:09
     */
    @PostMapping(value = "/getPerUserUnDoTaskList")
    @ApiOperation(value = "电子名片-营销人员个人超时未拜访次数排名接口", notes = "电子名片-营销人员个人超时未拜访次数排名接口")
    @ResponseBody
    @RsaRequest(param = true, result = true)
    public ResponseModel getPerUserUnDoTaskList(@RequestBody String json) {
        try {
            String salt = getSalt();//盐值
            //获取当前用户的信息
            EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(getCurrentUserId(), salt);
            JSONObject jsonObject = JSONObject.parseObject(json);
            int pageNum = jsonObject.getInteger("pageNum");
            int pageSize = jsonObject.getInteger("pageSize");
            Long appQueryDeptId = jsonObject.getLong("appQueryDeptId");//小程序传参查询用的deptId
            String userName = jsonObject.getString("userName");//小程序传参查询用的营销人员姓名
            String perType = jsonObject.getString("perType");//个人客户类型，1：农户，2：新市民
            if (StringUtils.objIsNullOrEmpty(pageNum) || StringUtils.objIsNullOrEmpty(pageSize)) {
                return ResponseModel.error(ResultStatus.PARAMETER_MISSING);
            }
            EcardBranchUserUndoTask ecardBranchUserUndoTask = new EcardBranchUserUndoTask();
            SysDept dept = sysDeptService.selectSecondLevelDeptByDeptId(userInfo.getDeptId());
            //是否只查业务条线（支行和分理处，不含总行各部室），0：是，1：否
            String ecardMerchantIsdept = sysConfigService.selectConfigByKey("ecard_merchant_isdept", dept.getDeptId());
            if ("0".equals(ecardMerchantIsdept)){
                ecardBranchUserUndoTask.setIsServiceLine(ecardMerchantIsdept);//是
            }
            SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(userInfo.getDeptId());//获取县级机构
            ecardBranchUserUndoTask.setDeptId(sysDept.getDeptId());
            ecardBranchUserUndoTask.setAppQueryDeptId(appQueryDeptId);
            ecardBranchUserUndoTask.setUserName(userName);
            ecardBranchUserUndoTask.setPerType(perType);//个人客户类型，1：农户，2：新市民
            PageHelper.startPage(pageNum, pageSize);
            List<EcardBranchUserUndoTask> ecardBranchUserUndoTaskList = ecardBranchPersonChartService.getPerUserUnDoTaskList(ecardBranchUserUndoTask);
            PageInfo pageInfo = new PageInfo(ecardBranchUserUndoTaskList);
            return ResponseModel.okToJSONArray(pageInfo.getTotal(), ecardBranchUserUndoTaskList);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败,请联系管理员!");
        }
    }
}
